package ch.zbw.pra.sysventory.application.controller;

import java.io.Serializable;
import java.sql.Timestamp;

import ch.zbw.pra.sysventory.dataaccess.dataAccessObject.ScanversionDataAccessObject;
import ch.zbw.pra.sysventory.model.Scanversion;
import ch.zbw.pra.sysventory.utils.DateUtils;

/**
 * Controller für den Home Panel
 * 
 * @author Ralph Koster
 */

public class HomePanelController implements Serializable {

	private static final long serialVersionUID = -1898773778151489009L;
	private ScanversionDataAccessObject scanversionDataAccessObject;
	
	private Scanversion scanversion;
	private boolean reloadScanversion;

	public HomePanelController() {
		this.scanversionDataAccessObject = new ScanversionDataAccessObject();
		loadLastScanversion();
	}
	
	public void setReloadScanversion(boolean reloadScanversion) {
		this.reloadScanversion = reloadScanversion;
	}

	public void loadLastScanversion() {
		scanversion = scanversionDataAccessObject.getLastScanversion();
		reloadScanversion = false;
	}
	
	public void reloadLastScanversion() {
		if(reloadScanversion) {
			loadLastScanversion();
		}
	}

	public String getLastScanStartDate() {
		String returnValue = "Keine Startzeit gefunden";
		Timestamp timestamp = scanversion.getScanStartTimestamp();
		if(null != timestamp) {
			returnValue = DateUtils.convertTimestampToDateTime(timestamp);
		}
		return returnValue;
	}

	public String getLastScanEndDate() {
		String returnValue = "Keine Endzeit gefunden";
		Timestamp timestamp = scanversion.getScanEndeTimestamp();
		if(null != timestamp) {
			returnValue = DateUtils.convertTimestampToDateTime(timestamp);
		}
		return returnValue;
	}

	public String getLastScanDuration() {
		String returnValue = "Keine Scanzeit gefunden";
		Timestamp scanStart = scanversion.getScanStartTimestamp();
		Timestamp scanEnd = scanversion.getScanEndeTimestamp();
		if(null != scanStart && null != scanEnd) {
			returnValue = DateUtils.calculateDurationFromTimestamps(scanStart, scanEnd);
		}
		return returnValue;
	}

	public String getLastScanNumber() {
		String returnValue = "Keine Scannummer gefunden";
		long scanNumber = scanversion.getNumberOfScanversion();
		if(0 != scanNumber) {
			returnValue = String.valueOf(scanNumber);
		}
		return returnValue;
	}
}
